package com.springboot.mp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.springboot.mp.entity.User;
import com.springboot.mp.mapper.UserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lzx
 * @Data: 2021/04/19/16:00
 * @Description：
 */
@RestController
@AllArgsConstructor
@Slf4j
public class UserController {
    private UserMapper userMapper;

    @GetMapping("/get")
    public Long save() {
        User user = new User();
        user.setName("字母哥");
        user.setAge(18);
        int row = userMapper.insert(user);
        log.info("影响记录数：{} ", row);
        log.info("雪花算法id:{}", user.getId());
        //主键默认采用雪花算法
        return user.getId();
        // insert into user(id,name,age) values (?,?,?)
    }

    @GetMapping("/getById")
    public User getById(Long id) {
        return userMapper.selectById(id);
        // select * from user where id = ?
    }

    @GetMapping("/listByMap")
    public List<User> listByMap() {
        Map<String, Object> map = new HashMap<>();
        //map的key指代的是mysql表中的列名，并非java实体的属性名
        map.put("name", "字母哥");
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::print);
        return users;
        // select *from user where name = ?
    }

    @GetMapping("/delete")
    public void delete(Long id) {
        int row = userMapper.deleteById(id);
        log.info("影响记录书：{}", row);
        // delete from user where id = ?
    }

    @GetMapping("/deleteByMap")
    public void deleteByMap() {
        //构造条件
        Map<String, Object> map = new HashMap<>();
        map.put("name", "字母哥");
        map.put("age", 18);
        int row = userMapper.deleteByMap(map);
        log.info("影响记录数：{}", row);
        // delete from user where name = ? and age = ?
    }

    @GetMapping("/list")
    public List<User> list() {
        return userMapper.selectList(null);
    }

    @GetMapping("/getByMap")
    public List<User> getByMap() {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.select("name", "age")
                .in("age", Arrays.asList(20, 30, 40))
                .last("limit 1");
        List<User> users = userMapper.selectList(query);
        users.forEach(System.out::print);
        return users;
    }

    @GetMapping("/findByMap")
    public List findByMap() {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.like("name", "J%")
                .lt("age", 40)
                .select("name", "age");
        List<Map<String, Object>> maps = userMapper.selectMaps(query);
        maps.forEach(System.out::print);
        return maps;
    }

    @GetMapping("/update")
    public void update() {
        //构造条件
        UpdateWrapper<User> update = new UpdateWrapper<>();
        update.eq("name", "Jack")
                .eq("age", 28);
        //构造更新值
        User user = new User();
        user.setAge(20)
                .setEmail("lingzhexi@163.com");

        int rows = userMapper.update(user, update);
        System.out.println(rows);
        // update user set age = ? ,email = ? where name = ? age =?
    }

}
